const {connection}=require('./sqlhelper')

module.exports={
    // 获取所有数据
    getAllComments(callback){
        let rql = `SELECT comments.id,comments.author,comments.content,posts.title,comments.created,comments.\`status\` FROM comments LEFT JOIN posts ON comments.post_id=posts.id`;
        connection.query(rql,(err,result)=>{
            err&&console.log(err);
            callback(result);
            // console.log(result);
        })
    },
    // 获取分页数据
    getCommentsBypage(pageIndex, pageSize,callback){
        let rql = `SELECT comments.id,comments.author,comments.content,posts.title,comments.created,comments.\`status\` FROM comments LEFT JOIN posts ON comments.post_id = posts.id LIMIT ${(pageIndex-1)*pageSize}, ${pageSize} `;
        connection.query(rql, (err, result) => {
            err && console.log(err);
            callback(result);
            // console.log(result);
        })
    },
    // 获取comments的总条数
    getCommentsCount(callback){
        let rql = `SELECT COUNT(id) as total FROM comments`;
        connection.query(rql, (err, result) => {
            err && console.log(err);
            callback(result[0]);
            // console.log(result);
        })
    },
    deleteCommentsById(id,callback){
        console.log(id);
        let sql = `DELETE FROM comments WHERE id='${id}'`;
        connection.query(sql,(err,result)=>{
            err&&console.log(err);
            callback(result);
        })
    }
}